package org.esprit.ocm.client.ui.one;

import org.esprit.ocm.client.rpc.oca.HostOcaService;
import org.esprit.ocm.client.rpc.oca.HostOcaServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class CreateHost extends Window {

	private final HostOcaServiceAsync	hostOcaService	= GWT.create(HostOcaService.class);

	public CreateHost(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {

		setTitle("Create Host");
		setWidth(300);
		setHeight(300);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(true);
		setShowMinimizeButton(false);
		centerInPage();

		final DynamicForm form1 = new DynamicForm();
		form1.setWidth(250);
		form1.setHeight(20);
		form1.setPadding(5);
		form1.setLayoutAlign(VerticalAlignment.TOP);

		final DynamicForm form2 = new DynamicForm();
		form2.setWidth(250);
		form2.setHeight(50);
		form2.setPadding(40);
		form2.setLayoutAlign(VerticalAlignment.TOP);

		final DynamicForm form3 = new DynamicForm();
		form3.setWidth(250);
		form3.setHeight(20);
		form3.setPadding(5);
		form3.setLayoutAlign(VerticalAlignment.TOP);

		VLayout vLayout = new VLayout();

		HLayout hLayout1 = new HLayout();
		hLayout1.setWidth("20%");
		HLayout hLayout2 = new HLayout();
		hLayout2.setWidth("50%");
		HLayout hLayout3 = new HLayout();
		hLayout3.setWidth("30%");

		vLayout.addMember(hLayout1);
		vLayout.addMember(hLayout2);
		vLayout.addMember(hLayout3);

		final TextItem name = new TextItem("name");
		name.setTitle("name");
		name.setRequired(true);

		Label label = new Label();
		label.setHeight(20);
		label.setLeft(30);
		label.setAlign(Alignment.CENTER);
		// label.setValign(VerticalAlignment.CENTER);
		label.setContents("<b>Drivers<b><hr>");
		label.draw();

		final ComboBoxItem CB1 = new ComboBoxItem();
		CB1.setTitle("Virtualization Manager:");
		CB1.setType("comboBox");
		CB1.setDefaultValue("KVM");
		CB1.setValueMap("KVM", "XEN", "EC2", "Dummy");

		final ComboBoxItem CB2 = new ComboBoxItem();
		CB2.setTitle("Information Manager:");
		CB2.setType("comboBox");
		CB2.setDefaultValue("KVM");
		CB2.setValueMap("KVM", "XEN", "EC2", "Dummy");

		final ComboBoxItem CB3 = new ComboBoxItem();
		CB3.setTitle("Transfer Manager:");
		CB3.setType("comboBox");
		CB3.setDefaultValue("KVM");
		CB3.setValueMap("KVM", "XEN", "EC2", "Dummy");

		ButtonItem createButton = new ButtonItem("create", "Create");
		createButton.setAlign(Alignment.CENTER);
		createButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {

				String secret = credentials.getAwsAccessKeyId() + ":" + credentials.getSecretAccessKey();
				String endpoint = server.getIpServer();
				String im = "im_" + CB2.getValue().toString().toLowerCase();
				String vmm = "vmm_" + CB1.getValue().toString().toLowerCase();
				String tm = "tm_" + CB3.getValue().toString().toLowerCase();
				
				hostOcaService.allocate(secret, endpoint, name.getValue().toString(), im, vmm, tm, new AsyncCallback<String>() {

					public void onSuccess(String s) {
						destroy();
						SC.warn(s);
					}

					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub

					}
				});
			}
		});

		form1.setFields(name);
		form1.redraw();
		form2.addChild(label);
		form2.setFields(CB1, CB2, CB3);
		form2.redraw();
		form3.setFields(createButton);
		hLayout1.addChild(form1);
		hLayout2.addChild(form2);
		hLayout3.addChild(form3);
		addItem(vLayout);

		draw();

	}
}
